package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.mapper.SetMealMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.MD5Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 预约业务层
 * @author: wangjingliang
 * @create: 2019-11-19 17:11
 **/
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImp implements OrderService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetMealMapper setMealMapper;

    /**
     * 在线预约
     * 首先要对预约日期进行判断,是否有这一天是否有设置预约日期
     * 接着对预约天查看是否已预约满
     * 然后检查是否同一天重复预约
     * 检查用户是否为会员,如果是,则跳过,如果不是,则进行自动注册
     * 最后预约成功,更新当日的已预约人数
     * 返回预约的id和预约的套餐
     *
     * @param:Map
     * @return:Map里面封装了预约id和预约套餐
     * @date: 2019/11/19
     */
    @Override
    public Map order(Map map) throws Exception {
        //首先要对预约日期进行判断,是否有提前预约
        String order_Date = (String) map.get("orderDate");
        Date orderDate = null;
        //将日期类型进行转换
        orderDate = DateUtils.parseString2Date(order_Date);

        //如果预约日期为空
        if (orderDate == null || orderDate.getTime() < System.currentTimeMillis()) {
            throw new RuntimeException("请选择正确的预约日期");
        }
        //一、查询这一天的预约设置,如果没有则返回 "本天并不可预约,请选择其它时间" ;
        // 如果有判断是否是今天,如果是今天则不可预约;
        // 如果不是判断这天的预约人数是否已经满了
        OrderSetting orderSetting = null;
        try {
            orderSetting = orderSettingMapper.findAllByDate(orderDate);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(MessageConstant.EXCEPION);
        }
        Date orderSettingDate = orderSetting.getOrderDate();
        //如果没有则返回 "本天并不可预约,请选择其它时间"
        if (orderSettingDate == null) {
            throw new RuntimeException(MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        String date = DateUtils.parseDate2String(new Date());
        //如果有判断是否是今天,如果是今天则不可预约
        if (date.equals(order_Date)) {
            throw new RuntimeException("今天不可预约");
        }

        //如果不是判断这天的预约人数是否已经满了
        if (orderSetting.getReservations() >= (orderSetting.getNumber() - 1)) {
            throw new RuntimeException(MessageConstant.ORDER_FULL);
        }
        //获取手机号
        String telephone = (String) map.get("telephone")+ RedisMessageConstant.SENDTYPE_ORDER;
        //根据手机号查询会员表
        Member member = null;
        try {
            member = memberMapper.findMemberByTelePhone(telephone);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(MessageConstant.EXCEPION);
        }
        String id = (String) map.get("setmealId");
        ;
        Integer idSetmeal = Integer.parseInt(id);
        String orderType = (String) map.get("orderType");

//对预约信息进行添加
        Order order = new Order();
        order.setOrderDate(orderDate);
        order.setOrderType(orderType);
        order.setSetmealId(idSetmeal);

        //根据返回的会员信息进行判断
        if (member != null) {
            Integer memberId = member.getId();
            //如果有返回信息则说明用户已经注册过了,那就通过用户的Id查询他的套餐表,获取套餐预约日期和套餐项目
            //如果是会员则进行id是封装
            order.setMemberId(memberId);
            int i = 0;
            //根据会员id查询预约表
            try {
                i = orderMapper.findCountByMemberId(order);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(MessageConstant.EXCEPION);
            }
            //判断是否是同一天,同一个套餐
            if (i == 1) {
                //同一天预约了相同的套餐,重复了
                throw new RuntimeException(MessageConstant.HAS_ORDERED);
            }
        } else {
            //没有注册会员,进行会员注册
            member = new Member();
            member.setName((String) map.get("name"));
            member.setSex((String) map.get("sex"));
            member.setPhoneNumber((String) map.get("telephone"));
            member.setIdCard((String) map.get("idCard"));
            //对密码进行加密
            member.setPassword(MD5Utils.md5("123456"));
            member.setRegTime(new Date());

            try {
                memberMapper.addAndReturnId(member);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(MessageConstant.EXCEPION);
            }
            Integer memberId = member.getId();
            //在完成会员注册后对预约信息的会员id进行封装
            order.setMemberId(memberId);

        }

        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //对预约进行套餐添加
        try {
            orderMapper.addAndReturnId(order);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(MessageConstant.EXCEPION);
        }

        int i = orderSetting.getReservations() + 1;

        //对添加预约人数
        try {
            orderSettingMapper.editReservationsByOrderDateAndReservations(orderDate, i);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(MessageConstant.EXCEPION);
        }
//根据套餐id查询套餐

        Setmeal setmeal = null;
        try {
            setmeal = setMealMapper.findSetmealById(idSetmeal);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(MessageConstant.EXCEPION);
        }


        Map<String, Object> hashmap = new HashMap<>();
        hashmap.put("orderId", order.getId());
        hashmap.put("setmealName", setmeal.getName());

        return hashmap;
    }


    /**
     * 根据id查询预约信息
     *
     * @param:int id
     * @return: Order
     * @date: 2019/11/20
     */
    @Override
    public Map findById(Integer id) {
        //根据预约id查询预约信息
        Order order = null;
        try {
            order = orderMapper.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(MessageConstant.EXCEPION);
        }
        //根据查询到的套餐id查询套餐信息
        Setmeal setmeal = null;
        try {
            setmeal = setMealMapper.findSetmealById(order.getSetmealId());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(MessageConstant.EXCEPION);
        }
        //根据查询到的用户id查询会员信息
        Member member = null;
        try {
            member = memberMapper.findById(order.getMemberId());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(MessageConstant.EXCEPION);
        }
        //对数据进行封装传回前台
        Map<String, Object> map = new HashMap<>();
        map.put("member", member.getName());
        map.put("setmeal", setmeal.getName());
        map.put("orderDate", order.getOrderDate());
        map.put("orderType", order.getOrderType());
        return map;
    }
}
